package com.example.springboot2209.Controller;

import com.example.springboot2209.entity.Article;
import com.example.springboot2209.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Name: ArticleController
 * @Date: 2023/10/28 16:46
 * @Author: 西风残叶
 */
@Controller
public class ArticleController {
    private static File articleDir;
    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()){
            articleDir.mkdir();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始动态处理");
        File[] files = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        // 将的到的文件传入到集合
        List<Article> articleList = new ArrayList<>();
        for (File file: files) {
            // 进行反序列化
            try (FileInputStream fis = new FileInputStream(file);
                 ObjectInputStream ois = new ObjectInputStream(fis);){
                Article article  = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        articleList.forEach(System.out::println);
        // 将内容写到网页中
        try {
            // 设置页面的字符集为utf-8
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border='1'>");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("</tr>");
            for (Article article: articleList) {
                pw.println("<tr>");
                pw.println("<td>"+article.getTitle()+"</td>");
                pw.println("<td>"+article.getAuthor()+"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章!!!!!!!!!!");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);
        if (title == null || title.isEmpty() || author == null ||
                author.isEmpty() || content == null || content.isEmpty()){
            try {
                response.sendRedirect("/article_fail.html");
                return;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        File file  = new File(articleDir,title+".obj");
        // 判断现在file是否存在如果已经存在则发表失败
        if (file.exists()){
            try {
                response.sendRedirect("article_fail.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        // 将发表的内容写入到文件
        Article article = new Article(title,author,content);
        try (FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos = new ObjectOutputStream(fos);){
            oos.writeObject(article);
            response.sendRedirect("/article_success.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
